import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { UntypedFormGroup, FormBuilder, UntypedFormControl, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { BehaviorSubject } from 'rxjs';
import { Consts } from 'src/app/consts/consts';
import { EditMode } from 'src/app/models/enums/edit-mode.enum';
import { AddPostParam } from 'src/app/models/param/add-post.param';
import { PostQueryParam } from 'src/app/models/param/post-query.param';
import { UpdatePostParam } from 'src/app/models/param/update-post.param';
import { PostDto } from 'src/app/models/sys/post.dto';
import { ApiService } from 'src/app/services/api.service';

const POST_NAME = 'postName';
const POST_CODE = 'postCode';
const ENABLED = 'enabled';
const POST_SORT = 'postSort';
const REMARK = 'remark';

@Component({
  selector: 'app-add-post',
  templateUrl: './add-post.component.html',
  styleUrl: './add-post.component.less'
})
export class AddPostComponent implements OnInit {
  @Input() modeSubject!: BehaviorSubject<{ mode: EditMode, post?: PostDto }>;

  @Output() addPostEvent = new EventEmitter<boolean>();

  // 新建还是编辑
  post?: PostDto;
  modeEnum = EditMode;
  editMode = EditMode.ADD;

  // form fields
  addForm!: UntypedFormGroup;
  submitted = false;

  constructor(private _api: ApiService,
    private _fb: FormBuilder,
    private _message: NzMessageService) { }

    ngOnInit(): void {
        this.createAddForm();
    }

  createAddForm() {
    this.addForm = this._fb.group({});
    this.addForm.addControl(POST_NAME, new UntypedFormControl(''));
    this.addForm.addControl(POST_CODE, new UntypedFormControl('', [Validators.minLength(2), Validators.maxLength(20)]));
    this.addForm.addControl(POST_SORT, new UntypedFormControl(''));
    this.addForm.addControl(ENABLED, new UntypedFormControl(''));
    this.addForm.addControl(REMARK, new UntypedFormControl('', [Validators.minLength(2), Validators.maxLength(20)]));

    this.modeSubject.subscribe((value) => {
      this.editMode = value.mode;

      if (this.editMode === EditMode.ADD) {
        this.addForm.get(POST_NAME)?.addValidators(Validators.required);
        this.addForm.get(POST_CODE)?.addValidators(Validators.required);
        this.addForm.get(POST_SORT)?.addValidators(Validators.required);
      } else {
        this.post = value.post;
        this.initParam(this.post!);
      }
    });
  }

  initParam(post: PostDto) {
    this.addForm.get(POST_NAME)?.patchValue(post.postName);
    this.addForm.get(POST_CODE)?.patchValue(post.postCode);
    this.addForm.get(POST_SORT)?.patchValue(post.postSort);
    this.addForm.get(ENABLED)?.patchValue(post.enabled);
    this.addForm.get(REMARK)?.patchValue(post.remark);
  }

  resetModal() {
    this.submitted = false;
  }

  get fm() {
    return this.addForm.controls;
  }

  onCreate() {
    this.submitted = true;
    if (!this.addForm.valid) {
      return;
    }

    const param = new AddPostParam();
    param.postName = this.addForm.get(POST_NAME)?.value;
    param.postCode = this.addForm.get(POST_CODE)?.value;
    param.remark = this.addForm.get(REMARK)?.value;
    param.postSort = this.addForm.get(POST_SORT)?.value;
    param.enabled = this.addForm.get(ENABLED)?.value;

    this._api.addPost(param).subscribe((resp) => {
      if (resp.code == Consts.RESP_OK) {
        this._message.info("添加岗位成功");
        this.resetModal();
        this.addPostEvent.emit(true);
      } else {
        this._message.warning("添加岗位失败");
      }
    });
  }

  onUpdate() {
    this.submitted = true;
    if (!this.addForm.valid) {
      return;
    }

    if (this.post == null) {
      this._message.warning("未选中岗位");
      return;
    }

    const param = new UpdatePostParam();
    param.postName = this.addForm.get(POST_NAME)?.dirty ? this.addForm.get(POST_NAME)?.value : undefined;
    param.postCode = this.addForm.get(POST_CODE)?.dirty ? this.addForm.get(POST_CODE)?.value : undefined;
    param.remark = this.addForm.get(REMARK)?.dirty ? this.addForm.get(REMARK)?.value : undefined;
    param.postSort = this.addForm.get(POST_SORT)?.dirty ? this.addForm.get(POST_SORT)?.value : undefined;
    param.enabled = this.addForm.get(ENABLED)?.dirty ? this.addForm.get(ENABLED)?.value : undefined;
    param.postId = this.post.postId;

    this._api.updatePost(param).subscribe((resp) => {
      if (resp.code == Consts.RESP_OK) {
        this._message.info("修改岗位成功");
        this.resetModal();
        this.addPostEvent.emit(true);
      } else {
        this._message.warning("修改岗位失败 " + resp.message);
      }
    });
  }

  onSubmit(event: UIEvent) {
    event.preventDefault();

    if (this.editMode === EditMode.ADD) {
      this.onCreate();
    } else {
      this.onUpdate();
    }
  }

  onCancel(event: UIEvent) {
    event.preventDefault();

    this.addPostEvent.emit(false);
  }
}
